package com.ml.mall.service.industry.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.req.industry.IndustryAddDto;
import com.ml.mall.bean.req.industry.IndustryQueryPageDto;
import com.ml.mall.bean.req.industry.IndustryUpdDto;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.entity.industry.Industry;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.exception.BizException;
import com.ml.mall.mapper.industry.IndustryMapper;
import com.ml.mall.service.industry.IndustryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class IndustryServiceImpl extends ServiceImpl<IndustryMapper, Industry> implements IndustryService {
    @Autowired
    private IndustryMapper industryMapper;

    //添加产业信息
    @Override
    public int addIndustry(IndustryAddDto dto) {
        //1.判断当前产业是否发生重复
        QueryWrapper<Industry> distinctWrapper = new QueryWrapper<>();
        distinctWrapper.eq("industry_name",dto.getIndustryName());
        int selectCount = industryMapper.selectCount(distinctWrapper);
        if (selectCount!=0){
            throw new BizException(CommonEnum.ALREADY_EXIST_SAME);
        }
        //2.执行添加产业操作
        Industry industry = new Industry();
        industry.setIndustryName(dto.getIndustryName());
        industry.setIndexNo(dto.getIndexNo());
        int insert = industryMapper.insert(industry);
        return insert;
    }

    //修改产业信息
    @Override
    public int updIndustry(IndustryUpdDto dto) {
        //1.判断当前产业名称是否发生重复
        QueryWrapper<Industry> distinctWrapper = new QueryWrapper<>();
        distinctWrapper.eq("industry_name",dto.getIndustryName())
                .ne("id",dto.getId());
        int selectCount = industryMapper.selectCount(distinctWrapper);
        if (selectCount!=0){
            throw new BizException(CommonEnum.ALREADY_EXIST_SAME);
        }
        //通过id查询产业状态，0为有效，1为无效
        Industry selectById = industryMapper.selectById(dto.getId());
        //当数据库中state为0 或者 数据库中状态为0并且传入数据state为0
        if (selectById.getState()==0 || (dto.getState()==0&&selectById.getState()!=0)){
            //执行更新操作
            Industry industry = new Industry();
            industry.setId(dto.getId());
            industry.setIndustryName(dto.getIndustryName());
            industry.setIndexNo(dto.getIndexNo());
            industry.setState(dto.getState());
            int updateById = industryMapper.updateById(industry);
            if (updateById==0){
                throw new BizException(CommonEnum.UPDATE_ERROR);
            }
            return updateById;
        }
        else{
            throw new BizException(CommonEnum.UPDATE_ERROR);
        }
    }

    //分页查询产业信息
    @Override
    public com.ml.mall.bean.page.Page<Industry> queryPageIndustry(IndustryQueryPageDto dto) {
        com.ml.mall.bean.page.Page<Industry> industryPageRes = new com.ml.mall.bean.page.Page<>(dto.getPageIndex(),dto.getPageNum());
        //将分页查询后的数据存入list集合中
        List<Industry> industryList = new ArrayList<>();
        //分页查询
        QueryWrapper<Industry> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(dto.getIndustryName()!=null,"industry_name",dto.getIndustryName())
                .eq(dto.getState()!=null,"state",dto.getState())
                .ne("id", CommonConsts.INDUSTRY_OTHER);
        IPage<Industry> page = new Page<>(dto.getPageIndex(),dto.getPageNum());
        page = industryMapper.selectPage(page, queryWrapper);
        log.info("queryPageIndustry.page===={}",page.toString());
        for(Industry industry:page.getRecords()){
            industryList.add(industry);
        }

        industryPageRes.setTotal((int) page.getTotal());
        industryPageRes.setRecords(industryList);
        return industryPageRes;
    }
}
